package com.cheyun.netsalev3.repository.aftersale

import com.cheyun.netsalev3.bean.aftersale.DriveParam
import com.cheyun.netsalev3.bean.aftersale.MaintenanceDetail
import com.cheyun.netsalev3.mock.AfterSale
import com.cheyun.netsalev3.utils.api.ApiCallback
import com.cheyun.netsalev3.utils.api.ApiClient
import com.cheyun.netsalev3.utils.api.ApiException
import com.cheyun.netsalev3.utils.api.BaseResp
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlin.reflect.KFunction1

class MaintenanceDetailsRepository(var id: Int) {
    /**
     * 获取详情
     */
    fun getData(ReturnData: KFunction1<@ParameterName(name = "item") MaintenanceDetail, Unit>) {
        ApiClient.getInstance().getMaintenanceDetail(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<MaintenanceDetail>>() {
                    override fun onSuccess(t: BaseResp<MaintenanceDetail>) {
                        t.data?.apply {
                            ReturnData(this)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    fun cancel(mid: Int,
               moth: () -> Unit, fail: () -> Unit) {
        ApiClient.getInstance().cancelMaintenance(mid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }

    fun cancelInfo(vin: String,
               moth: (total:Int) -> Unit) {
        ApiClient.getInstance().cancelMaintenanceInfo(vin)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DriveParam>>() {
                    override fun onSuccess(t: BaseResp<DriveParam>) {
                        t.data.apply {
                            moth(this?.total!!)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }

                })
    }

    fun confirm(mid: Int,
               moth: () -> Unit, fail: () -> Unit) {
        ApiClient.getInstance().confirmMaintenance(mid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }

    fun allocation(mid: String,serciveuid:String,
                moth: () -> Unit, fail: () -> Unit) {
        ApiClient.getInstance().allocationMaintenance(mid,serciveuid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }


}